package com.dmagic.cms.common.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.ArrayUtils;

/**
 * <h3>Class name</h3> Array Tools <h4>Description</h4> <h4>Special Notes</h4>
 * for more
 * 
 * @see org.apache.commons.collections.ListUtils
 * @version 0.1
 * @author songlin.li 2012-5-29
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class ListUtil extends ArrayUtils {
    public static final List EMPTY_LIST = Collections.EMPTY_LIST;

    /**
     * @param collection
     * @return
     */

    public static boolean isEmpty(Collection collection) {
        if (collection == null || collection.isEmpty()) {
            return true;
        }
        return false;
    }

    /**
     * @param collection
     * @return
     */
    public static boolean isNotEmpty(Collection collection) {
        return !isEmpty(collection);
    }

    /**
     * @description :remove Collection Duplicate values
     * 
     * @author songlin.li
     * @param list
     * @return
     */
    public static <T> List<T> removeDuplicateBySet(List<T> source) {
        Set set = new HashSet();
        List distinctResult = new ArrayList();
        for (Iterator iter = source.iterator(); iter.hasNext();) {
            Object element = iter.next();
            if (set.add(element))
                distinctResult.add(element);
        }
        source.clear();
        source.addAll(distinctResult);
        return source;
    }

    /**
     * @description : must be generic remove Collection Duplicate values
     * 
     * @author songlin.li
     * @param list
     * @return
     */
    public static <T> List<T> removeDuplicateByContains(List<T> list) {
        if (isEmpty(list)) {
            return null;
        }
        List<T> distinctResult = new ArrayList();
        for (T t : list) {
            if (!distinctResult.contains(t)) {
                distinctResult.add(t);
            }
        }
        return distinctResult;
    }

    /**
     * @description : Arrays.asList() return java.util.Arrays$ArrayList are not return
     * ArrayList, Arrays$ArrayList and ArrayList are extends AbstractList,
     * remove,add Etc. method at AbstractList is defaults throw
     * UnsupportedOperationException and not do and works return
     * Arrays.asList(array); this method is return ArrayList
     * 
     * @author songlin.li
     * @param array
     * @return
     */
    public static <T> List<T> arrayToList(T[] array) {
        List list = new ArrayList();
        if (array != null) {
            for (int i = 0; i < array.length; i++) {
                list.add(array[i]);
            }
        }
        return list;
    }

    /**
     * @description : Description of the method
     * 
     * @author songlin.li
     * @param list
     * @return
     */
    public static <T> T[] listToArray(List<T> list) {
        return (T[]) list.toArray();
    }

    /**
     * @description : get first result set
     * 
     * @author songlin.li
     * @param list
     * @return
     */
    public static <T> T unique(List<T> list) {
        if (isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }
}
